package jzqb.service.impl;


import haha.life.common.BelongToSqlUtils;
import haha.life.common.CutPageBean;
import haha.life.common.PermissionTypeTool;
import haha.life.common.SysTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import haha.life.normal.TokenUtils;
import jzqb.bo.QBFWAuditBo;
import jzqb.bo.QBFWBo;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.IQBFWDao;
import jzqb.po.*;
import jzqb.service.ILCService;
import jzqb.service.IQBFWService;
import jzqb.vo.QBFWAuditVo;
import jzqb.vo.QBFWUserVo;
import jzqb.vo.QBFWVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/6/21 16:59
 * @Version 1.0
 */
@Service
public class QBFWServiceImpl implements IQBFWService {
    @Autowired
    private IQBFWDao qbfwDao;
    @Autowired
    private AdminUserInfoDao userInfoDao;
    @Autowired
    private ILCService lcService;
    //上传附件临时存放地址
    @Value("${filePath.fileTemPResource}")
    private String fileTemPResource;
    //情报服务文件正式存放地址
    @Value("${filePath.qbfwFilePath}")
    private String qbfwFilePath;


    @Override
    public Boolean addQBFW(QBFWPo qbfwPo) {
        String applicantId = qbfwPo.getApplicant();
        AdminUserPo user = userInfoDao.findUserById(applicantId);
        //申请人    111_姓名
        String realApplicant = applicantId + "_" + user.getName();
        qbfwPo.setApplicant(realApplicant);
        //文件临时存放路径
        String filePath = qbfwPo.getAnnexPath();
        //设置当前节点:等待第一个审核人 审核
        qbfwPo.setCurrentNode(1);
        //更新时间默认为空
        qbfwPo.setUpdateTime("");
        qbfwPo.setCreateTime(SysTool.getCurrentTime(SysTool.dateFormat_milli));
        Boolean addQBFWBoolean = qbfwDao.insertQBFW(qbfwPo);
        if (!addQBFWBoolean) {
            return false;
        }
        //部门主管
        AdminDepartmentPo deptLeader = qbfwDao.findDeptLeader(qbfwPo.getApplicationDept());
        //情报主管信息
        AdminDepartmentPo qbDeptLeader = qbfwDao.findDeptLeader(SysTool.YQTBMB_QBID);
        //情报部门id
        String qxzId = "";
        if ("资源申请".equals(qbfwPo.getApplicationType())) {
            // qxzId = SysTool.QBFW_ZiYuanShenPiZu;
            qxzId = PermissionTypeTool.front_businessManageGroupId;
        } else if ("权限申请".equals(qbfwPo.getApplicationType())) {
            //  qxzId = SysTool.QBFW_QuanXianShenPiZu;
            qxzId = PermissionTypeTool.front_SysManageGroupId;
        }
        //权限组
        AdminPermissionGroupPo qxz = qbfwDao.findQXZBbyQXZId(qxzId);
        //情报管理员_组目标
        String qbManagerTargetGroup = qxz.getDepartments();
        //情报管理员_用户目标
        String qbManagerTargetUser = qxz.getUsers();

        //获取新建记录的id
        Integer maxId = qbfwDao.getQBFWMaxId();
        //部门主管id
        String deptLeaderId = "";
        //情报部门主管id
        String qbDeptLeaderId = "";
        if (deptLeader != null) {
            deptLeaderId = deptLeader.getLeaderIDs();
        }
        if (qbDeptLeader != null) {
            qbDeptLeaderId = qbDeptLeader.getLeaderIDs();
        }
        //新建三个审批流程
        Boolean addThreeLcBoolean = lcService.addThreeLc(maxId, deptLeaderId, qbDeptLeaderId, qbManagerTargetUser, qbManagerTargetGroup);
        //挪动文件到正式目录
        if (!"".equals(filePath) && filePath != null) {//附件不为空
            if (addQBFWBoolean && addThreeLcBoolean) { //存放成功
                String oldPath = fileTemPResource + "\\" + filePath;
                String newPath = qbfwFilePath + "\\" + filePath;
                Boolean moveFileBoolean = false;
                try {
                    moveFileBoolean = FileuploadUtils.fileMove(oldPath, newPath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return addQBFWBoolean && addThreeLcBoolean && moveFileBoolean;
            }
        }
        //附件为空,直接返回
        return addQBFWBoolean && addThreeLcBoolean;
    }

    @Override
    public Boolean checkDeptLeader(String applicationDept) {
        //部门
        AdminDepartmentPo deptLeader = qbfwDao.findDeptLeader(applicationDept);
        if (deptLeader == null || "".equals(deptLeader.getLeaderIDs())) {//部门为空或者领导为空
            return false;
        }
        return true;
    }

    @Override
    public String qbfwUploading(MultipartFile file, String basePath) {
        String savePath = FileuploadUtils.uploading(file, basePath);
        return savePath;
    }

    @Override
    public Object qbfwBatchUploading(String allFolderPath, HttpServletRequest request) {
        Object obj = FileuploadUtils.fileBatchUploading(allFolderPath, request);
        return obj;
    }

    @Override
    public Boolean qbfwDelFolder(Map map) {
        String filePath = (String) map.get("filePath");
        Integer isTemp = (Integer) map.get("isTemp"); //是否临时文件(0:否  1:是)
        String[] split = filePath.split("\\\\");
        String delFolderPath = "";
        if (isTemp == 0) { //不是临时文件
            delFolderPath = qbfwFilePath + "\\" + split[0] + "\\" + split[1] + "\\" + split[2];
        } else if (isTemp == 1) { //是临时文件
            delFolderPath = fileTemPResource + "\\" + split[0] + "\\" + split[1] + "\\" + split[2];
        }
        return FileUtil.deleteFolder(delFolderPath);
    }

    @Override
    public QBFWUserVo getCurrentUser(String currentUserId) {
        QBFWUserVo qbfwUserVo = new QBFWUserVo();
        AdminUserPo user = userInfoDao.findUserById(currentUserId);
        String departmentId = user.getDepartmentid();
        String[] spit = departmentId.split(";");
        String belongToSql = BelongToSqlUtils.getBelongToSql(spit, "部门编号");
        List<AdminDepartmentPo> deptList = qbfwDao.findDeptListByBelongToSqL(belongToSql);
        qbfwUserVo.setCurrentUserId(user.getId());
        qbfwUserVo.setCurrentUserName(user.getName());
        qbfwUserVo.setCurrentUserEmail(user.getEmail());
        qbfwUserVo.setCurrentUserTel(user.getPhone());
        qbfwUserVo.setDepartmentList(deptList);

        return qbfwUserVo;
    }

    @Override
    public CutPageBean<QBFWPo> complexQueryQBFW(QBFWAuditBo qbfwAuditBo, HttpServletRequest request) {
        Integer flag = qbfwAuditBo.getFlag();
        CutPageBean<QBFWPo> cutPageBean = null;
        if (flag == 0) { //查询自己创建的情报服务
//            String currentUserId = TokenUtils.getCurrentUserId(request);
//            qbfwAuditBo.setApplicant(currentUserId);
            cutPageBean = this.queryQBFW(qbfwAuditBo, request);
        } else if (flag == 1) { //查询待审列表
            cutPageBean = this.showQBFWAuditList(qbfwAuditBo, request);
        } else if (flag == 2) { //查询已审列表
            cutPageBean = this.showHaveReadQBFWAuditList(qbfwAuditBo, request);
        }
        return cutPageBean;
    }

    @Override
    public QBFWVo showSingleQBFW(Integer qbfwId) {
        QBFWVo qbfwVo = qbfwDao.showSingleQBFW(qbfwId);
        String deptId = qbfwVo.getApplicationDept();
        String applyerID = qbfwVo.getApplicant().split("_")[0];
        QBFWUserVo qbfwUser = this.getCurrentUser(applyerID);
        qbfwVo.setUserVo(qbfwUser);
        AdminDepartmentPo dept = qbfwDao.findDeptNameByDeptId(deptId);
        if (dept != null) {
            qbfwVo.setApplyerDeptName(dept.getDepartmentName());
        }
        return qbfwVo;
    }

    @Override
    public CutPageBean<QBFWPo> showQBFWAuditList(QBFWAuditBo qbfwAuditBo, HttpServletRequest request) {
        CutPageBean<QBFWPo> cutPageBean = new CutPageBean<>();
        qbfwAuditBo.setStart(qbfwAuditBo.getPage(), qbfwAuditBo.getPageSize());

        String currentUserId = TokenUtils.getCurrentUserId(request);
        //获取 1  2  3节点的sql
        String codeOneSql = this.getCodeOneSql(currentUserId);
        String codeTwoSql = this.getCodeTwoSql(currentUserId);
        String codeThreeSql = this.getCodeThreeSql(currentUserId);
        String overSql = this.ConstructOverSql(codeOneSql, codeTwoSql, codeThreeSql);
        qbfwAuditBo.setStatus(1);
        qbfwAuditBo.setOverSql(overSql);
        qbfwAuditBo.setSearchType(qbfwAuditBo.getSearchType());
        qbfwAuditBo.setSearchValue(qbfwAuditBo.getSearchValue());
        List<QBFWPo> qbfwPoList = qbfwDao.showQBFWAuditList(qbfwAuditBo);
        int count = (int) qbfwDao.showQBFWAuditCount(qbfwAuditBo);
        cutPageBean.initCutPage(count, qbfwAuditBo.getPageSize(), qbfwPoList);
        return cutPageBean;
    }

    @Override
    public CutPageBean<QBFWPo> showHaveReadQBFWAuditList(QBFWAuditBo qbfwAuditBo, HttpServletRequest request) {
        CutPageBean<QBFWPo> cutPageBean = new CutPageBean<>();
        qbfwAuditBo.setStart(qbfwAuditBo.getPage(), qbfwAuditBo.getPageSize());
        String currentUserId = TokenUtils.getCurrentUserId(request);
        //获取 1  2  3节点的sql
        String codeOneSql = this.getCodeOneSql(currentUserId);
        String codeTwoSql = this.getCodeTwoSql(currentUserId);
        String codeThreeSql = this.getCodeThreeSql(currentUserId);
        String overSql = this.ConstructOverSql(codeOneSql, codeTwoSql, codeThreeSql);
        qbfwAuditBo.setOverSql(overSql);
        qbfwAuditBo.setSearchType(qbfwAuditBo.getSearchType());
        qbfwAuditBo.setSearchValue(qbfwAuditBo.getSearchValue());
        List<QBFWPo> qbfwPoList = qbfwDao.showHaveReadQBFWAuditList(qbfwAuditBo);
        int count = (int) qbfwDao.showHaveReadQBFWAuditCount(qbfwAuditBo);
        cutPageBean.initCutPage(count, qbfwAuditBo.getPageSize(), qbfwPoList);
        return cutPageBean;
    }

    /**
     * 获取节点1 sql
     *
     * @param currentUserId
     * @return
     */
    public String getCodeOneSql(String currentUserId) {
        //节点1   是那些部门的主管(是不是申请部门的主管)
        String codeOne = "";
        List<AdminDepartmentPo> depts = qbfwDao.findDeptsByLeader(currentUserId);
        StringJoiner deptStr = new StringJoiner(";");
        if (depts != null && !depts.isEmpty()) {
            for (AdminDepartmentPo dept : depts) {
                deptStr.add(dept.getDepartmentCode());
            }
            String[] deptSplit = String.valueOf(deptStr).split(";");
            String applyDeptBelongToSql = BelongToSqlUtils.getBelongToSql(deptSplit, "申请部门");
            codeOne = "(当前节点=1  and  " + applyDeptBelongToSql + ")";
        }
        return codeOne;
    }

    /**
     * 获取节点2 sql
     *
     * @param currentUserId
     * @return
     */
    public String getCodeTwoSql(String currentUserId) {
        //节点2  是不是情报部门主管
        String codeTwo = "";
        AdminDepartmentPo department = new AdminDepartmentPo();
        department.setLeaderIDs(currentUserId);
        department.setDepartmentCode(SysTool.YQTBMB_QBID);
        AdminDepartmentPo isQbDeptLeader = qbfwDao.findIsQbDeptLeader(department);
        if (isQbDeptLeader != null) {
            codeTwo += "(当前节点=2)";
        }
        return codeTwo;
    }

    /**
     * 获取节点3 sql
     *
     * @param currentUserId
     * @return
     */
    public String getCodeThreeSql(String currentUserId) {
        //节点3  是不是 资源申请组或权限申请组的成员
        String codeThree = "";
        //查询用户信息
        AdminUserPo user = userInfoDao.findUserById(currentUserId);
        //用户所在部门
        String userDept = user.getDepartmentid();
        String[] userDeptSplit = userDept.split(";");

        String userDeptBelongToSql = BelongToSqlUtils.getBelongToSql(userDeptSplit, "部门成员") + " or  ";
        //组建用于查询是否资源审批组成员的Sql
        // String ziyuanSql = "id=" + SysTool.QBFW_ZiYuanShenPiZu + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
        String ziyuanSql = "id=" + PermissionTypeTool.front_businessManageGroupId + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
        //组建用于查询是否权限审批组成员的Sql
        // String quanxianSql = "id=" + SysTool.QBFW_QuanXianShenPiZu + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
        String quanxianSql = "id=" + PermissionTypeTool.front_SysManageGroupId + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
        //3.1 是否为 资源审批组成员
        AdminPermissionGroupPo ziyuanGroup = qbfwDao.findIsPermissionGroupMember(ziyuanSql);
        //3.2 是否为 权限审批组成员
        AdminPermissionGroupPo quanxianGroup = qbfwDao.findIsPermissionGroupMember(quanxianSql);
        if (ziyuanGroup != null && quanxianGroup != null) {//是资源审批组成员  也是权限审批组成员
            codeThree += "(当前节点 = 3 )";
        } else if (ziyuanGroup != null && quanxianGroup == null) {//是资源审批组成员
            codeThree += "(当前节点 = 3  and 申请类型='资源申请')";
        } else if (quanxianGroup != null && ziyuanGroup == null) {//是权限审批组成员
            codeThree += "(当前节点 = 3  and 申请类型='权限申请')";
        }
        return codeThree;
    }

    /**
     * 穷举三个节点存在不存在的情况
     *
     * @param codeOne   节点1
     * @param codeTwo   节点2
     * @param codeThree 节点3
     * @return
     */
    public String ConstructOverSql(String codeOne, String codeTwo, String codeThree) {
        String overSql = "";
        if (!"".equals(codeOne) && "".equals(codeTwo) && "".equals(codeThree)) { //1不为空,2.3为空
            overSql = codeOne;
        } else if ("".equals(codeOne) && !"".equals(codeTwo) && "".equals(codeThree)) { //2不为空,1.3为空
            overSql = codeTwo;
        } else if ("".equals(codeOne) && "".equals(codeTwo) && !"".equals(codeThree)) { //3不为空,1.2为空
            overSql = codeThree;
        } else if (!"".equals(codeOne) && !"".equals(codeTwo) && "".equals(codeThree)) {//1.2不为空,3为空
            overSql = "(" + codeOne + " or " + codeTwo + ")";
        } else if (!"".equals(codeOne) && "".equals(codeTwo) && !"".equals(codeThree)) {//1.3不为空,2为空
            overSql = "(" + codeOne + " or " + codeThree + ")";
        } else if ("".equals(codeOne) && !"".equals(codeTwo) && !"".equals(codeThree)) {//2.3不为空,1为空
            overSql = "(" + codeTwo + " or " + codeThree + ")";
        } else if (!"".equals(codeOne) && !"".equals(codeTwo) && !"".equals(codeThree)) {//1.2.3都不为空
            overSql = "(" + codeOne + " or " + codeTwo + " or " + codeThree + ")";
        }
        return overSql;
    }


    @Override
    public QBFWAuditVo showSingleQBFWAudit(Integer qbfwId) {
        QBFWAuditVo qbfwAudit = new QBFWAuditVo();
        QBFWVo qbfwVo = qbfwDao.showSingleQBFW(qbfwId);
        if (qbfwVo != null) {
            QBFWUserVo qbfwUserVo = new QBFWUserVo();
            //申请部门
            String applyDept = qbfwVo.getApplicationDept();
            AdminDepartmentPo dept = qbfwDao.findDeptNameByDeptId(applyDept);
            qbfwVo.setApplyerDeptName(dept.getDepartmentName());
            //申请人 格式为:  111_张三
            String applyer = qbfwVo.getApplicant();
            String applyerId = applyer.split("_")[0];
            //组装用户信息
            AdminUserPo user = userInfoDao.findUserById(applyerId);
            qbfwUserVo.setCurrentUserId(user.getId());
            qbfwUserVo.setCurrentUserName(user.getName());
            qbfwUserVo.setCurrentUserTel(user.getPhone());
            qbfwUserVo.setCurrentUserEmail(user.getEmail());
            //用户所在部门集合
            String deptStr = user.getDepartmentid();
            String[] spit = deptStr.split(";");
            String belongToSql = BelongToSqlUtils.getBelongToSql(spit, "部门编号");
            List<AdminDepartmentPo> deptList = qbfwDao.findDeptListByBelongToSqL(belongToSql);
            qbfwUserVo.setDepartmentList(deptList);
            //附件路径
            String annexPath = qbfwVo.getAnnexPath();
            if (!"".equals(annexPath) && annexPath != null) {
                String realPath = qbfwFilePath + "\\" + annexPath;
                byte[] fileBinary = FileUtil.fileToBinArray(realPath);
                qbfwVo.setFileByte(fileBinary);
            }
            //注入值
            qbfwVo.setUserVo(qbfwUserVo);
            qbfwAudit.setQbfwInfo(qbfwVo);
        }

        List<LCPo> threeLcList = qbfwDao.getThreeLcByQBFWId(qbfwId);
        for (LCPo lc : threeLcList) {
            //操作人ID
            String OperatorID = lc.getOperator();
            switch (lc.getNode()) {
                case 1://节点1
                    qbfwAudit.setFirstLc(lc);
                    if (!"".equals(OperatorID) && OperatorID != null) {//设置操作人姓名
                        AdminUserPo Operator1 = userInfoDao.findUserById(OperatorID);
                        if (Operator1 != null) {
                            lc.setOperator(Operator1.getName());
                        }

                    }
                    break;
                case 2://节点2
                    qbfwAudit.setSecondLc(lc);
                    if (!"".equals(OperatorID) && OperatorID != null) {//设置操作人姓名
                        AdminUserPo Operator2 = userInfoDao.findUserById(OperatorID);
                        if (Operator2 != null) {
                            lc.setOperator(Operator2.getName());
                        }
                    }
                    break;
                case 3://节点3
                    String annexPath = lc.getAnnexPath();
                    String lcAnnexPath = qbfwFilePath + "\\" + annexPath;
                    if (!"".equals(annexPath) && annexPath != null) {
                        byte[] annexByte = FileUtil.fileToBinArray(lcAnnexPath);
                        lc.setAnnexByte(annexByte);
                    }
                    qbfwAudit.setThirdLc(lc);
                    if (!"".equals(OperatorID) && OperatorID != null) {//设置操作人姓名
                        AdminUserPo Operator3 = userInfoDao.findUserById(OperatorID);
                        if (Operator3 != null) {
                            lc.setOperator(Operator3.getName());
                        }
                    }
                    break;
            }
        }
        return qbfwAudit;
    }

    @Override
    public Object qbfwLcAudit(LCPo lcPo, HttpServletRequest request) {
        //当前用户
        String currentUserId = TokenUtils.getCurrentUserId(request);
        lcPo.setOperator(currentUserId);
        QBFWPo qbfwPo = new QBFWPo();
        qbfwPo.setId(lcPo.getApplicationId());
        Integer currentNode = lcPo.getNode();
        AdminDepartmentPo dept = new AdminDepartmentPo();
        switch (currentNode) {
            case 1:
                //查询是否为申请部门的主管
                QBFWVo qbfwOne = qbfwDao.showSingleQBFW(lcPo.getApplicationId());
                String applyDeptId = qbfwOne.getApplicationDept();//申请部门
                dept.setDepartmentCode(applyDeptId);
                dept.setLeaderIDs(currentUserId);
                AdminDepartmentPo isDeptLeader = qbfwDao.findDeptByDeptIdAndLeaderId(dept);
                if (isDeptLeader == null) { //如果不是部门领导 直接返回
                    return "权限不足";
                }

                if (lcPo.getStatus() == 1) { //审核通过
                    qbfwPo.setStatus(1);
                    qbfwPo.setCurrentNode(2);
                } else if (lcPo.getStatus() == 2) {//审核不通过
                    qbfwPo.setStatus(2);
                    qbfwPo.setCurrentNode(1);
                }
                break;
            case 2:
                //查询是否为情报部门的主管
                dept.setLeaderIDs(currentUserId);
                dept.setDepartmentCode(SysTool.YQTBMB_QBID);
                AdminDepartmentPo isQbDeptLeader = qbfwDao.findIsQbDeptLeader(dept);
                if (isQbDeptLeader == null) { //如果不是情报部门领导 直接返回
                    return "权限不足";
                }
                if (lcPo.getStatus() == 1) { //审核通过
                    qbfwPo.setStatus(1);
                    qbfwPo.setCurrentNode(3);
                } else if (lcPo.getStatus() == 2) {//审核不通过
                    qbfwPo.setStatus(2);
                    qbfwPo.setCurrentNode(2);
                }
                break;
            case 3:
                //查询是否为 资源申请组或权限申请组的成员
                QBFWVo qbfwTwo = qbfwDao.showSingleQBFW(lcPo.getApplicationId());
                String applyType = qbfwTwo.getApplicationType();//申请类型
                AdminUserPo user = userInfoDao.findUserById(currentUserId);//用户信息
                //用户所在部门
                String userDept = user.getDepartmentid();
                String[] userDeptSplit = userDept.split(";");
                String userDeptBelongToSql = BelongToSqlUtils.getBelongToSql(userDeptSplit, "部门成员") + " or  ";
                AdminPermissionGroupPo permissionGroupPo = null;
                if ("资源申请".equals(applyType)) {
                    //组建用于查询是否资源审批组成员的Sql
                    // String ziyuanSql = "id=" + SysTool.QBFW_ZiYuanShenPiZu + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
                    String ziyuanSql = "id=" + PermissionTypeTool.front_businessManageGroupId + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
                    //3.1 是否为 资源审批组成员
                    permissionGroupPo = qbfwDao.findIsPermissionGroupMember(ziyuanSql);
                } else if ("权限申请".equals(applyType)) {
                    //组建用于查询是否权限审批组成员的Sql
                    // String quanxianSql = "id=" + SysTool.QBFW_QuanXianShenPiZu + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
                    String quanxianSql = "id=" + PermissionTypeTool.front_SysManageGroupId + " and (" + userDeptBelongToSql + " 用户成员=" + currentUserId + ")";
                    //3.2 是否为 权限审批组成员
                    permissionGroupPo = qbfwDao.findIsPermissionGroupMember(quanxianSql);
                }
                if (permissionGroupPo == null) {
                    return "权限不足";
                }
                if (lcPo.getStatus() == 1) { //审核通过
                    qbfwPo.setStatus(3);
                } else if (lcPo.getStatus() == 2) {//审核不通过
                    qbfwPo.setStatus(2);
                }
                break;
        }
        Boolean updateQBFWNode = qbfwDao.updateQBFWNode(qbfwPo);
        Boolean updateLcBoolean = lcService.updateLc(lcPo);
        //附件临时路径
        String annexPath = lcPo.getAnnexPath();
        if (updateQBFWNode && updateLcBoolean && !"".equals(annexPath) && annexPath != null) {
            String oldPath = fileTemPResource + "\\" + annexPath;
            String newPath = qbfwFilePath + "\\" + annexPath;
            try {
                FileuploadUtils.fileMove(oldPath, newPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return updateLcBoolean && updateQBFWNode;
    }

    @Override
    public CutPageBean<QBFWPo> queryQBFW(QBFWBo qbfwBo, HttpServletRequest request) {
        String currentUserId = TokenUtils.getCurrentUserId(request);
        //查询用户信息
        AdminUserPo user = userInfoDao.findUserById(currentUserId);
        qbfwBo.setApplicant(currentUserId + "_" + user.getName());
        CutPageBean<QBFWPo> cutPageBean = new CutPageBean<>();
        List<QBFWPo> qbfwList = qbfwDao.queryQBFWList(qbfwBo);
        int count = (int) qbfwDao.queryQBFWCount(qbfwBo);
        cutPageBean.initCutPage(count, qbfwBo.getPageSize(), qbfwList);
        return cutPageBean;
    }

    @Override
    public Boolean delQBFWAndAnnexPath(QBFWPo qbfwPo) {
        Integer id = qbfwPo.getId();
        //所携带附件的路径
        String annexPath = qbfwPo.getAnnexPath();
        //删除情报服务
        Boolean delBoolean = qbfwDao.delQBFWById(id);
        if (!delBoolean || "".equals(annexPath) || annexPath == null)
            return delBoolean;
        String[] split = annexPath.split("\\\\");
        String folderPath = qbfwFilePath + "\\" + split[0] + "\\" + split[1] + "\\" + split[2];
        //删除所携带的附件
        Boolean delFolder = FileUtil.deleteFolder(folderPath);
//        return delBoolean && delFolder;
        return delBoolean;
    }


    @Override
    public Boolean updateQBFW(QBFWBo qbfwBo) {
        //原本的记录
        QBFWPo oldQbfw = qbfwDao.showSingleQBFW(qbfwBo.getId());
        //原本的文件存放地
        String oldAnnexPath = oldQbfw.getAnnexPath();
        String updateTime = SysTool.getCurrentTime(SysTool.dateFormat_milli);
        qbfwBo.setUpdateTime(updateTime);
        Boolean updateBoolean = qbfwDao.updataQBFW(qbfwBo);//修改成功
        //是否修改了附件  0:未修改  1:修改了
        Integer isUpdateAnnexPath = qbfwBo.getIsUpdateAnnexPath();
        //删除原有的附件
        if (updateBoolean && isUpdateAnnexPath == 1) { //修改记录成功且修改了附件
            //删除原有的文件
            if (!"".equals(oldAnnexPath) && oldAnnexPath != null) {
                String[] split = oldAnnexPath.split("\\\\");
                String oldFolderPath = qbfwFilePath + "\\" + split[0] + "\\" + split[1] + "\\" + split[2];
                //删除原来记录所携带的附件
                FileUtil.deleteFolder(oldFolderPath);
            }
            //将文件从临时存放路径移动到正式存放路径
            String filePath = qbfwBo.getAnnexPath();
            if (!"".equals(filePath) && filePath != null) { //修改的文件不为空
                String fromPath = fileTemPResource + "\\" + filePath;
                String toPath = qbfwFilePath + "\\" + filePath;
                Boolean moveFileBoolean = false;
                try { //将上传的文件,从临时目录移动到正式目录
                    moveFileBoolean = FileuploadUtils.fileMove(fromPath, toPath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return updateBoolean && moveFileBoolean;
            }
        }
        return updateBoolean;
    }

    @Override
    public Boolean updateRejectQBFWO(QBFWBo qbfwBo) {
        String applyerId = qbfwBo.getApplicant();
        AdminUserPo user = userInfoDao.findUserById(applyerId);
        String overApplyer = applyerId + "_" + user.getName();
        qbfwBo.setApplicant(overApplyer);
        Integer status = qbfwBo.getStatus();
        if (status == 0) { //暂存
            qbfwBo.setEditorStatus(0);
        } else if (status == 1) { //提交
            qbfwBo.setEditorStatus(1);
        }
        qbfwBo.setStatus(1);
        qbfwBo.setCurrentNode(1);
        //修改被打回的内容
        Boolean updateQBFWBoolean = this.updateQBFW(qbfwBo);
        Boolean delThreeLcBoolean = false;
        //删除原有对应的三条审核流程
        if (updateQBFWBoolean) {
            delThreeLcBoolean = lcService.delThreeLc(qbfwBo.getId());
        }
        //部门主管
        AdminDepartmentPo deptLeader = qbfwDao.findDeptLeader(qbfwBo.getApplicationDept());
        //情报主管信息
        AdminDepartmentPo qbDeptLeader = qbfwDao.findDeptLeader(SysTool.YQTBMB_QBID);
        //情报部门id
        String qxzId = "";
        if ("资源申请".equals(qbfwBo.getApplicationType())) {
            // qxzId = SysTool.QBFW_ZiYuanShenPiZu;
            qxzId = PermissionTypeTool.front_businessManageGroupId;
        } else if ("权限申请".equals(qbfwBo.getApplicationType())) {
            // qxzId = SysTool.QBFW_QuanXianShenPiZu;
            qxzId = PermissionTypeTool.front_SysManageGroupId;
        }
        //权限组
        AdminPermissionGroupPo qxz = qbfwDao.findQXZBbyQXZId(qxzId);
        //情报管理员_组目标
        String qbManagerTargetGroup = qxz.getDepartments();
        //情报管理员_用户目标
        String qbManagerTargetUser = qxz.getUsers();
        //部门主管id
        String deptLeaderId = deptLeader.getLeaderIDs();
        //情报部门主管id
        String qbDeptLeaderId = qbDeptLeader.getLeaderIDs();
        //新建三个审批流程
        Boolean addThreeLcBoolean = lcService.addThreeLc(qbfwBo.getId(), deptLeaderId, qbDeptLeaderId, qbManagerTargetUser, qbManagerTargetGroup);
        return updateQBFWBoolean && delThreeLcBoolean && addThreeLcBoolean;
    }

    @Override
    public int findNewQBFWAuditData(HttpServletRequest request) {
        QBFWAuditBo qbfwAuditBo = new QBFWAuditBo();
        String currentUserId = TokenUtils.getCurrentUserId(request);
        //获取 1  2  3节点的sql
        String codeOneSql = this.getCodeOneSql(currentUserId);
        String codeTwoSql = this.getCodeTwoSql(currentUserId);
        String codeThreeSql = this.getCodeThreeSql(currentUserId);
        String overSql = this.ConstructOverSql(codeOneSql, codeTwoSql, codeThreeSql);
        qbfwAuditBo.setStatus(1);
        qbfwAuditBo.setOverSql(overSql);
        int count = (int) qbfwDao.showQBFWAuditCount(qbfwAuditBo);
        return count;
    }
}

